package com.wuyi.tea.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyi.tea.domain.dto.ProductDTO;
import com.wuyi.tea.domain.pojo.Cart;
import com.wuyi.tea.domain.pojo.CartProduct;
import com.wuyi.tea.domain.pojo.Product;
import com.wuyi.tea.domain.vo.ProductVO;
import com.wuyi.tea.mapper.CartMapper;
import com.wuyi.tea.mapper.CartProductMapper;
import com.wuyi.tea.mapper.ProductMapper;
import com.wuyi.tea.service.CartProductService;
import com.wuyi.tea.service.CartService;
import com.wuyi.tea.util.LocalStorageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private CartMapper cartMapper;
    @Resource
    private CartProductMapper cartProductMapper;
    @Resource
    private ProductMapper productMapper;

    @Override
    public List<ProductVO> getCartProductList() {

        List<ProductVO> res = new ArrayList<>();

        // 1.获取当前用户id
        Long userId = Long.valueOf(LocalStorageUtil.get().toString());

        // 2. 获取当前用户购物车id
        Cart cart = cartMapper.selectOne(new LambdaQueryWrapper<Cart>()
                        .eq(Cart::getUserId,userId)
        );

        if (Objects.isNull(cart)){

            cart = new Cart();
            cart.setUserId(userId);
            cartMapper.insert(cart);
            return Collections.emptyList();
        }

        // 3. 根据购物车id获取所有商品id
        List<CartProduct> cartProductList = cartProductMapper.selectList(
                new LambdaQueryWrapper<CartProduct>()
                    .eq(CartProduct::getCartId,cart.getId())
        );

        // 4.根据所有商品id获取商品具体信息
        for (CartProduct cartProduct : cartProductList) {

            Product product = productMapper.selectById(cartProduct.getProductId());

            ProductVO productVO = ProductVO.builder()
                    .id(cartProduct.getProductId())
                    .count(cartProduct.getProductCount())
                    .name(product.getName())
                    .build();

            // 查询出当前商品的价格
            productVO.setPrice(product.getPrice());

            // 5.计算每件商品的 总价格 = 单价 * 数量
            productVO.setTotalPrice(productVO.getPrice() * productVO.getCount());

            // 设置主图片
            productVO.setMainPicture(product.getMainPicture());

            productVO.setCategoryId(product.getCategoryId());

            res.add(productVO);
        }

        return res;
    }

    @Override
    public void deleteCartProductList(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)){
            throw new RuntimeException("要删除的商品为空");
        }

        for (Integer id : ids) {
            cartProductMapper.delete(new LambdaQueryWrapper<CartProduct>()
                    .eq(CartProduct::getProductId,id)
            );
        }
    }

    @Transactional
    @Override
    public void addCartProduct(ProductDTO productDTO) {

        if (Objects.isNull(productDTO.getId())){
            throw new RuntimeException("商品id为null");
        }

        if (Objects.isNull(productDTO.getCount())){
            throw new RuntimeException("商品数量为空");
        }

        // 1. 获取当前用户id
        Long userId = Long.valueOf(LocalStorageUtil.get().toString());

        // 2. 获取用户购物车id
        Cart cart = cartMapper.selectOne(
                new LambdaQueryWrapper<Cart>()
                        .eq(Cart::getUserId,userId)
        );

        if (Objects.isNull(cart)){
            cart = Cart.builder()
                    .userId(userId)
                    .build();
            cartMapper.insert(cart);
        }

        //  判断商品原先是否存在
        CartProduct cartProduct = cartProductMapper.selectOne(new LambdaQueryWrapper<CartProduct>()
                .eq(CartProduct::getCartId,cart.getId())
                .eq(CartProduct::getProductId,productDTO.getId())
        );

        if (Objects.nonNull(cartProduct)){

            cartProduct.setProductCount(cartProduct.getProductCount() + productDTO.getCount());
            cartProductMapper.updateById(cartProduct);
            return;
        }

        // 3. 向购物车与商品的关联表中添加商品数据
        cartProduct = CartProduct.builder()
                .productId(productDTO.getId())
                .productCount(productDTO.getCount())
                .cartId(cart.getId())
                .build();
        cartProductMapper.insert(cartProduct);
    }
}
